package org.mtmi.ui.controls.edits;

import java.util.ArrayList;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.mtmi.ui.M;
import org.mtmi.ui.actions.Action;
import org.mtmi.ui.actions.IAction;
import org.mtmi.ui.controls.Control;
import org.mtmi.ui.controls.SideButton;
import org.mtmi.ui.controls.listeners.PopupListener;
import org.mtmi.ui.controls.popups.AbstractButtonPopup;
import org.mtmi.ui.drawables.IDrawable;
import org.mtmi.ui.drawables.SymbolDrawable;
import org.mtmi.ui.scenes.CLR;
import org.mtmi.ui.scenes.COLOR;
import org.mtmi.ui.scenes.ChooseSideButtonActionPopup;
import org.mtmi.ui.scenes.GlobalSceneSideActionButton;
import org.mtmi.ui.scenes.Scene;
import org.mtmi.ui.scenes.SceneManager;

/**
 * This action add a SideButton to control scene.
 * 
 */
public class AddSideButtonAction extends Action
{
	private static class Drawable implements IDrawable
	{

		@Override
		public Point computeSize(int width, int height) {
			return new Point( Math.max( 24, width), Math.max( 24, height ) );
		}

		@Override
		public void paint( GC gc, int x, int y, int width, int height, int state )
		{
			gc.setLineWidth( 2 );
			if( state == NORMAL ) {
				gc.setForeground( COLOR.yellow );
				gc.setBackground( CLR.darker( COLOR.yellow, 0.5f ) );
			} else {
				gc.setForeground( COLOR.yellow );
				gc.setBackground( CLR.lighter( COLOR.yellow, 0.5f ) );
			}
			int d = Math.min( width, height );
			int nx = x+(width-d)/2;
			int ny = y+(height-d)/2;
			SideButton.paint( gc, nx, ny, d, M.TOP|M.LEFT );
			
			//a green + on top right corner, this an add button!
			int s = 12;
			gc.setLineWidth( 0 );
			SymbolDrawable sd = new SymbolDrawable( COLOR.green, SymbolDrawable.PLUS );
			sd.paint( gc, nx+d-s/2-2, ny-s/2-2, s, s, state );
		}
	}
	
	private IDrawable d;
	
	public AddSideButtonAction() {
		super( PUSH );
		d = new Drawable();
	}
	
	@Override
	public boolean isEnabled() { return true; }
	
	@Override
	public IDrawable getImage() {
		return d;
	} 
	
	@Override
	public void run() {
		final ChooseSideButtonActionPopup p = new ChooseSideButtonActionPopup( SceneManager.getDefault().getScene(), M.SCENE_EDITOR_CONTROL );
		p.addResponseListener( new PopupListener() {
			@Override
			public void popupButtonSelected(int id) {
				if( id == AbstractButtonPopup.OK ) {
					createSideButton( p.getSelectedAction() );
				}
			}
		});
	}
	
	private void createSideButton( IAction action ) 
	{
		Scene scene = SceneManager.getDefault().getScene();
		
		ArrayList<SideButton> sbs = SideButton.getSideButtons( scene );
		
		//try to put it on corners:
		int corners[] = new int[] { 
		    M.TOP|M.LEFT, M.TOP|M.RIGHT, M.BOTTOM|M.LEFT, M.BOTTOM|M.RIGHT };
		for( int ic=0; ic<corners.length; ic ++ ) {
			boolean corner_free = true;
			int nf = corners[ic];
			for( SideButton sb : sbs ) {
				int of = sb.getFlags();
				if( (of & M.DIRECTION_MASK) == nf ) {
					corner_free=false;
					break;
				}
			}
			if( corner_free ) {
				//this corner is free !
				GlobalSceneSideActionButton b = new GlobalSceneSideActionButton( scene, action, nf );
				return ;
			}
		}
		
		//by default place it on LEFT side just under (TOP/LEFT corner):
		//user will move it...
		GlobalSceneSideActionButton b = new GlobalSceneSideActionButton( scene, action, M.LEFT );
		b.setLocation( b.getLocation().x, 60 );
	}
}
